package com.cjj;

import ohos.agp.components.AttrHelper;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Arc;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.Rect;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;

/**
 * Created by cjj on 2016/2/20.
 */
public class SunFaceView extends Component implements Component.EstimateSizeListener, Component.DrawTask {

    private static final String Tag = SunFaceView.class.getSimpleName();
    private static final int DEFAULT_SUN_RADIUS = 12;//太阳的半径
    private static final int DEFAULT_EYES_RADIUS = 2;//眼睛的半径

    private int mHeight, mWidth;
    private Paint mCirclePaint;//画圆的画笔
    private int mSunRadius;//sun radius
    private int mEyesRadius = DEFAULT_EYES_RADIUS;
    private Rect debugRect;
    private RectFloat mouthRect;
    private int mSunColor;//sun color
    private boolean isDrawFace = true;
    private int mouthStro = 3;

    public SunFaceView(Context context) {
        this(context, null);
    }

    public SunFaceView(Context context, AttrSet attrs) {
        this(context, attrs, 0);
    }

    public SunFaceView(Context context, AttrSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
        System.out.println("719------init(1)");
        setLayoutRefreshedListener(new LayoutRefreshedListener() {

            @Override
            public void onRefreshed(Component component) {
                onSizeChanged(component.getWidth(), component.getHeight());
            }
        });
        setEstimateSizeListener(this::onEstimateSize);
        addDrawTask(this::onDraw);
    }

    private void init() {
        HiLog.info(Contants.LABEL, "init");

        mSunRadius = changeDp(DEFAULT_SUN_RADIUS);
        mEyesRadius = changeDp(DEFAULT_EYES_RADIUS);

        //圆的配置
        mCirclePaint = new Paint();
        mCirclePaint.setAntiAlias(true); // 抗锯齿
        mCirclePaint.setColor(Color.RED);
        mCirclePaint.setStyle(Paint.Style.FILL_STYLE);

        debugRect = new Rect();
        mouthRect = new RectFloat();
    }

    /**
     * 设置太阳半径
     *
     * @param sunRadius 半径
     */
    public void setSunRadius(int sunRadius) {
        mSunRadius = changeDp(sunRadius);
        invalidate();
    }

    /**
     * 设置眼睛大小
     *
     * @param eyesSize 大小
     */
    public void setEyesSize(int eyesSize) {
        mEyesRadius = changeDp(eyesSize);
        invalidate();
    }

    /**
     * 设置嘴巴粗细
     *
     * @param mouthStro 粗细
     */
    public void setMouthStro(int mouthStro) {
        this.mouthStro = mouthStro;
        invalidate();
    }


    /**
     * 刷新用的效果
     *
     * @param sunRadius 半径
     * @param per 大小
     */
    public void setPerView(int sunRadius, float per) {
        sunRadius = changeDp(sunRadius);
        if (per >= 0.8) {
            isDrawFace = true;
        } else {
            isDrawFace = false;
        }
        per = Math.min(per, 1);
        float tempRadius = sunRadius * per;
        mSunRadius = (int) tempRadius;
        mCirclePaint.setAlpha((int) per * 255);
        invalidate();
    }

    /**
     * 设置sun color
     *
     * @param sunColor 颜色
     */
    public void setSunColor(int sunColor) {
        mSunColor = sunColor;
        invalidate();
    }

    protected void onSizeChanged(int w, int h) {
        HiLog.info(Contants.LABEL, "w---->" + w + "  -------  h----->" + h);
        System.out.println("719-----------onSizeChanged");
//        mWidth = w;
//        mHeight = h;

        debugRect.left = mWidth / 2 - mSunRadius;
        debugRect.right = mWidth / 2 + mSunRadius;
        debugRect.top = mHeight / 2 - mSunRadius;
        debugRect.bottom = mHeight / 2 + mSunRadius;


//        mouthRect.left = mWidth / 2 - mSunRadius / 2;
//        mouthRect.right = mWidth / 2 + mSunRadius / 2;
//        mouthRect.top = mHeight / 2 - mSunRadius / 2;
//        mouthRect.bottom = mHeight / 2 + mSunRadius / 2;
    }

    @Override
    public boolean onEstimateSize(int widthMeasureSpec, int heightMeasureSpec) {
        HiLog.info(Contants.LABEL, "onMeasure");
        System.out.println("719-----------onEstimateSize");
        int widthMode = EstimateSpec.getMode(widthMeasureSpec);
        int widthSize = EstimateSpec.getSize(widthMeasureSpec);
        int heightMode = EstimateSpec.getMode(heightMeasureSpec);
        int heightSize = EstimateSpec.getSize(heightMeasureSpec);
        int width;
        int height;
        if (widthMode == EstimateSpec.PRECISE) {
            width = widthSize;
        } else {
            width = mSunRadius * 2 + getPaddingRight() + getPaddingLeft();
        }

        if (heightMode == EstimateSpec.PRECISE) {
            height = heightSize;
        } else {

            height = mSunRadius * 2 + getPaddingTop() + getPaddingBottom();
        }

        setEstimatedSize(width, height);

        mWidth = width;
        mHeight = height;
        return false;
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        System.out.println("719-----------onDraw");
        drawCircle(canvas);
        mCirclePaint.setStyle(Paint.Style.STROKE_STYLE);
        //        canvas.drawRect(debugRect, mCirclePaint);
        //        canvas.drawRect(mouthRect, mCirclePaint);
    }

    private void drawCircle(Canvas canvas) {
        mCirclePaint.setColor(new Color(mSunColor));
        mCirclePaint.setStyle(Paint.Style.FILL_STYLE);
        canvas.drawCircle(mWidth / (float) 2, mHeight / (float) 2, mSunRadius, mCirclePaint);
        mCirclePaint.setColor(Color.WHITE);
        if (isDrawFace) {
            mouthRect.left = mWidth / 2 - mSunRadius / 2;
            mouthRect.right = mWidth / 2 + mSunRadius / 2;
            mouthRect.top = mHeight / 2 - mSunRadius / 2;
            mouthRect.bottom = mHeight / 2 + mSunRadius / 2;

            canvas.save();
            canvas.drawCircle(mWidth / 2 - mSunRadius / 2 + mEyesRadius, mHeight / 2 - mSunRadius / 2 + mEyesRadius, mEyesRadius, mCirclePaint);
            canvas.drawCircle(mWidth / 2 + mSunRadius / 2 - mEyesRadius, mHeight / 2 - mSunRadius / 2 + mEyesRadius, mEyesRadius, mCirclePaint);
            mCirclePaint.setStyle(Paint.Style.STROKE_STYLE);
            mCirclePaint.setStrokeWidth(mouthStro);
            canvas.drawArc(mouthRect, new Arc(20, 140, false), mCirclePaint);
            canvas.restore();
        }
    }

    public int changeDp(int value) {
        return AttrHelper.vp2px(value, getContext());
    }
}
